package com.java.multipledatasource.thread;

import com.java.multipledatasource.spring.item.ChildItem;
import org.springframework.util.ClassUtils;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * @author hu
 * @version 1.0
 * @date 2021/1/20 0020 上午 8:46
 * @description
 */
public class Fibonacci extends RecursiveTask<Integer> {
    int n;

    public Fibonacci(int n) {
        this.n = n;
    }

    @Override
    public Integer compute() {
        if (n <= 1) {
            return n;
        }
        Fibonacci f1 = new Fibonacci(n - 1);
        f1.fork();
        Fibonacci f2 = new Fibonacci(n - 2);
        f2.fork();
        return f1.join() + f2.join();
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        for (int i = 0; i < 10; i++) {
            ForkJoinTask task = forkJoinPool.submit(new Fibonacci(i));
            System.out.println(task.get());
            System.err.println(fib(i));
        }

        ClassLoader classLoader = ClassUtils.class.getClassLoader();
        ClassLoader dynamicLoader = classLoader;
        String path = "com.java.multipledatasource.spring.item.ChildItem";
        Class<?> aClass = ClassUtils.forName(path, dynamicLoader);
        ChildItem childItem = (ChildItem) aClass.newInstance();
    }

    public static int fib(int num) {
        if (num <= 1) {
            return num;
        }
        return fib(num - 1) + fib(num - 2);
    }
}
